﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OS.BLL;
using OS.Common;

namespace DownloadPlug_ins
{
    public partial class frmCar : Form
    {
        public CommonMethod commonMethod = new CommonMethod();
        public frmCar()
        {
            InitializeComponent();
        }

        private void gbtnSPDownLoad_Click(object sender, EventArgs e)
        {
            string filename = this.ctxtFileName.Text.Trim();

            string[] lines = System.IO.File.ReadAllLines(filename);

            int count = 0;

            List<SpModel> ls = new List<SpModel>();

            SpModel model = new SpModel();
            int index = 1;
            foreach (string line in lines)
            {
              
             
                int a = 0;
                if (int.TryParse(line, out a) == false) //判断是否可以转换为整型
                {
                    var line1 = line.Split('\t');
                   
                    if (line1.Length > 7)
                    {
                        int num = 1;
                        try
                        {
                            num = int.Parse(line1[7]);
                        }
                        catch (Exception ex) { LogHelper.m_CreateErrorLogTxt(model.SPBH, index.ToString(), "数量有错误:" + ex.Message); }
                        model.SL = num;
                        ls.Add(model);
                        index++;
                    }
                }
                else
                {
                    if (index == int.Parse(line))
                    {
                        model = new SpModel();
                    }
                    else
                        model.SPBH = line;
                }


              
            }

            if (ls.Count > 0)
            {
                foreach (SpModel entity in ls)
                {
                int count1=    OrderProduct(entity.SPBH, entity.SL.ToString());
                if (count1 > 0)
                    count++;
                }
            }
            MessageBox.Show("导入成功 "+count+"条记录！");

        }
        public int   OrderProduct(string tmh, string sl)
        {
            int ss = 0;
            try
            {
                Bll_web_cart webCart = new Bll_web_cart();
                dmdaH_Entity dmdaHModel = new dmdaH_Entity();

                dmdaHModel.Dmbh = "9008";
                string userSn = dmdaHModel.Dmbh;
                string time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

                bool kcsl = commonMethod.IsExistDqck(tmh);

                int status = 0;

                if (kcsl  )
                    status = 1;
                if (webCart.Exists(tmh, userSn))
                {
                    ss = commonMethod.UpdateCart(tmh, sl, userSn, time, status);
                }
                else
                {
                    int hh = webCart.GetNewCartHH(userSn);
                    ss = commonMethod.AddCart(tmh, sl, userSn, time, hh, status);
                }
            }
            catch (Exception ex)
            {
                LogHelper.m_CreateErrorLogTxt("OrderProduct", "订购商品", ex.Message);
            }
            return ss;
        }

        private void gbtnQuery_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开";
            //ofd.Filter = "*.xlsx|*.xls";
          ofd.Filter = "文本文件|*.txt";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.ctxtFileName.Text = ofd.FileName;
            }
        }

        private void pSet_Click(object sender, EventArgs e)
        {
            Config config = new Config();
            config.ShowDialog();
        }

    }
}
